package HZTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Hang
 * @date 2022/9/22
 * @project AutumnOffer
 * @description
 **/
public class lc56 {
    public static void main(String[] args) {
       int[][] intervals = new int[][]{{1,3},{2,6},{8,10},{15,18}};
        int[][] merge = merge(intervals);
        System.out.println(merge(intervals));
    }
    public static int[][] merge(int[][] intervals) {
        if(intervals.length == 0) return new int[][]{{0,0}};
        List<int[]> res = new ArrayList<>();
        Arrays.sort(intervals,(o1,o2)->o1[0]-o2[0]);
        for(int i=0;i<intervals.length;i++){
            if(res.size()==0||res.get(res.size()-1)[1]<intervals[i][0]){
                res.add(new int[]{intervals[i][0],intervals[i][1]});
            }else {
                res.get(res.size()-1)[1] = Math.max(res.get(res.size()-1)[1],intervals[i][1]);
            }
        }
        return res.toArray(new int[res.size()][2]);
    }
}
